<%!

  Copyright 2016 Facebook, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

%><%#service:functions%><%#function:returnType%>
template <typename Protocol_>
void <%service:name%>AsyncClient::<%function:name%>T(Protocol_* prot, bool useSync, apache::thrift::RpcOptions& rpcOptions, std::unique_ptr<apache::thrift::RequestCallback> callback<%function:comma%><% > service_common/function_param_list_const%>) {
  struct HeaderAndConnContext {
    HeaderAndConnContext() : header(apache::thrift::transport::THeader::ALLOW_BIG_FRAMES) {}

    apache::thrift::transport::THeader header;
    apache::thrift::Cpp2ConnContext connContext;
  };
  auto headerAndConnContext = std::make_shared<HeaderAndConnContext>();
  std::shared_ptr<apache::thrift::transport::THeader> header(headerAndConnContext, &headerAndConnContext->header);
  header->setProtocolId(getChannel()->getProtocolId());
  header->setHeaders(rpcOptions.releaseWriteHeaders());
  headerAndConnContext->connContext.setRequestHeader(header.get());
  std::unique_ptr<apache::thrift::ContextStack> ctx = this->getContextStack(this->getServiceName(), "<%service:name%>.<%function:name%>", &headerAndConnContext->connContext);
<%#function:takes_stream?%>
  <%service:name%>_<%function:name%>_pargs::FieldsType args;
<%/function:takes_stream?%>
<%^function:takes_stream?%>
  <%service:name%>_<%function:name%>_pargs args;
<%/function:takes_stream?%>
<%#function:args_without_streams%><%#field:type%>
<%^type:resolves_to_complex_return?%>
  args.get<<%field:index%>>().value = &<%field:name%>;
<%/type:resolves_to_complex_return?%>
<%#type:resolves_to_complex_return?%>
  args.get<<%field:index%>>().value = const_cast<<% > types/type%>*>(&<%field:name%>);
<%/type:resolves_to_complex_return?%>
<%/field:type%><%/function:args_without_streams%><%!

%><%#function:args%><%#field:type%><%#first?%>
<%#type:resolves_to_stream?%>
  #error Service functions don't allow stream input yet
<%/type:resolves_to_stream?%>
<%/first?%><%/field:type%><%/function:args%><%!

%><%^service:frozen2?%>
  auto sizer = [&](Protocol_* p) { return args.serializedSizeZC(p); };
  auto writer = [&](Protocol_* p) { args.write(p); };
<%/service:frozen2?%>
<%#service:frozen2?%>
  auto sizer = [&](Protocol_* p) { return ::apache::thrift::detail::serializedResponseBodySizeZC(p, &args); };
  auto writer = [&](Protocol_* p) { ::apache::thrift::detail::serializeResponseBody(p, &args); };
<%/service:frozen2?%>
<%#function:oneway?%>
  apache::thrift::clientSendT<Protocol_>(prot, rpcOptions, std::move(callback), std::move(ctx), header, channel_.get(), "<%function:name%>", writer, sizer, apache::thrift::RpcKind::SINGLE_REQUEST_NO_RESPONSE, useSync);
<%/function:oneway?%><%!

%><%^function:oneway?%>
<%#function:returns_stream?%>
  apache::thrift::clientSendT<Protocol_>(prot, rpcOptions, std::move(callback), std::move(ctx), header, channel_.get(), "<%function:name%>", writer, sizer, apache::thrift::RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE, useSync);
<%/function:returns_stream?%>
<%^function:returns_stream?%>
  apache::thrift::clientSendT<Protocol_>(prot, rpcOptions, std::move(callback), std::move(ctx), header, channel_.get(), "<%function:name%>", writer, sizer, apache::thrift::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE, useSync);
<%/function:returns_stream?%>
<%/function:oneway?%>
  headerAndConnContext->connContext.setRequestHeader(nullptr);
}

<%/function:returnType%><%/service:functions%>
